package com.authine.cloudpivot.engine.plugin.service.impl;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.YAMLConfig;
import com.authine.cloudpivot.engine.enums.type.CorpRelatedType;
import com.authine.cloudpivot.engine.plugin.service.impl.wechatmsg.SendWechatMSG;
import com.authine.cloudpivot.engine.service.dto.AbstractMessageDTO;
import com.authine.cloudpivot.engine.service.dto.SimpleMsgDTO;
import com.authine.cloudpivot.engine.service.message.IMessageService;
import com.authine.cloudpivot.engine.service.organization.UserService;
import com.authine.cloudpivot.engine.service.organization.exception.DataSourceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.authine.cloudpivot.engine.domain.organization.User;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuN
 * @description 11
 * @date 2020/8/17
 */
@Service
@Slf4j
public class IMessageServiceImpl implements IMessageService {

    @Autowired
    private UserService userService;

    @Autowired
    private SendSMTPMsg sendSMTPMsg;

    @Autowired
    private SendWechatMSG sendWechatMsg;

    @Value("${smtpvalues.accountName}")
    private String account;
    @Value("${smtpvalues.passwordValue}")
    private String password;
    @Value("${smtpvalues.mail_smtp_host}")
    private String mail_smtp_host;

    @Value("${smtpvalues.mail_smtp_ssl_trust}")
    private String mail_smtp_ssl_trust;

    @Value("${smtpvalues.mail_smtp_port}")
    private String mail_smtp_port;

    @Value("${smtpvalues.mail_smtp_auth}")
    private String mail_smtp_auth;

    @Value("${smtpvalues.mail_smtp_starttls_enable}")
    private String mail_smtp_starttls_enable;

    @Value("${smtpvalues.base_url}")
    private String base_url;

    @Value("${smtpvalues.end_url}")
    private String end_url;
    @Value("${smtpvalues.end_over_url}")
    private String end_over_url;
    @Value("${smtpvalues.approveNotifyStartText}")
    private String approveNotifyStartText;
    @Value("${smtpvalues.approveNotifyEndText}")
    private String approveNotifyEndText;
    @Value("${smtpvalues.overNotifyStartText}")
    private String overNotifyStartText;
    @Value("${smtpvalues.overNotifyEndText}")
    private String overNotifyEndText;

    @Value("${wechatvalues.corpid}")
    private String corpid;
    @Value("${wechatvalues.corpsecret}")
    private String corpsecret;
    @Value("${wechatvalues.agengid}")
    private String ageng_id;
    @Value("${wechatvalues.wx_token_url}")
    private String wx_token_url;
    @Value("${wechatvalues.wx_msg_url}")
    private String wx_msg_url;
    @Value("${wechatvalues.wx_user_url}")
    private String wx_user_url;
    @Value("${wechatvalues.approveWxNotifyText}")
    private String approveWxNotifyText;
    @Value("${wechatvalues.overWxNotifyStartText}")
    private String overWxNotifyStartText;
    @Value("${wechatvalues.sendWX}")
    private boolean sendWX;
    @Value("${smtpvalues.sendEmail}")
    private boolean sendEmail;


    public YAMLConfig setConfigValue()
    {
        YAMLConfig config = new YAMLConfig();
        config.setAccount(account);
        config.setPassword(password);
        config.setBase_url(base_url);
        config.setEnd_url(end_url);
        config.setEnd_over_url(end_over_url);
        config.setMail_smtp_auth(mail_smtp_auth);
        config.setMail_smtp_host(mail_smtp_host);
        config.setMail_smtp_port(mail_smtp_port);
        config.setMail_smtp_ssl_trust(mail_smtp_ssl_trust);
        config.setMail_smtp_starttls_enable(mail_smtp_starttls_enable);
        config.setApproveNotifyStartText(approveNotifyStartText);
        config.setApproveNotifyEndText(approveNotifyEndText);
        config.setOverNotifyStartText(overNotifyStartText);
        config.setOverNotifyEndText(overNotifyEndText);
        config.setCorpid(corpid);
        config.setCorpsecret(corpsecret);
        config.setAgeng_id(ageng_id);
        config.setWx_token_url(wx_token_url);
        config.setWx_msg_url(wx_msg_url);
        config.setWx_user_url(wx_user_url);
        config.setApproveWxNotifyText(approveWxNotifyText);
        config.setOverWxNotifyStartText(overWxNotifyStartText);
        config.setSendWX(sendWX);
        return config;
    }


    @Override
    public List<String> sendMsg(AbstractMessageDTO abstractMessageDTO) throws DataSourceException {
        //接收对象
        YAMLConfig config = setConfigValue();
        List<String> receiverLists= abstractMessageDTO.getReceiverList();
        String bizParamsString = abstractMessageDTO.getBizParams();
        BizParams bizParams = JSON.parseObject(bizParamsString, BizParams.class);
        String approveWorkItemIdURL = "form/detail?workitemId="+bizParams.getWorkItemId()+"&";
        String overWorkItemIdURL = "form/detail?";
        String workflowInstanceIdURL = "workflowInstanceId="+bizParams.getWorkflowInstanceId()+"&";
        String url = "";
        List<String> emailList=new ArrayList<>();
        List<String> wxAccountList=new ArrayList<>();
        User user= new User();
        if(receiverLists.size()>0){
            //Get user by Username, although the method is decrepted, But this still working fine for our get user
            user=userService.getByUsername(receiverLists.get(0),true);
        }
        String content=abstractMessageDTO.getContent();
        String wxContent = content;

        if (abstractMessageDTO.getTitle().indexOf("流程结束") >=0)
        {
            url = base_url + overWorkItemIdURL + workflowInstanceIdURL + end_over_url;
            content = content +"</br>"+config.getOverNotifyStartText() +" <a href=" + url + ">link</a> " +config.getOverNotifyEndText();
            wxContent ="Your process\n"+ wxContent +"\n"+config.getOverWxNotifyStartText();
        }
        else
        {
            url = base_url + approveWorkItemIdURL + workflowInstanceIdURL + end_url;;
            content = content +"</br>"+config.getApproveNotifyStartText() +" <a href=" + url + ">link</a> " +config.getApproveNotifyEndText();
            wxContent = "The process\n" +wxContent +"\n"+config.getApproveWxNotifyText();
        }
        if (sendWX && user!=null)
        {
            sendWechatMsg.sendWXMSG(config,user.getOfficePhone(),"text",wxContent);
        }
        if (sendEmail && user!=null)
        {
            sendSMTPMsg.sendEmail(user.getEmail(), abstractMessageDTO.getTitle(), content, url, config);
        }
        return null;
    }

    @Override
    public List<String> sendSimpleTextMsg(SimpleMsgDTO simpleMsgDTO) throws DataSourceException {
        return null;
    }

    @Override
    public String initChannel()
    {
        return CorpRelatedType.OTHER.toString();
    }


}
